package com.uit3j.cloud.product.service.impl;

import com.uit3j.cloud.api.dto.HotSearchDTO;
import com.uit3j.cloud.api.product.bo.EsHotSearchBO;
import com.uit3j.cloud.common.cache.constant.CacheNames;
import com.uit3j.cloud.common.database.dto.PageDTO;
import com.uit3j.cloud.common.database.util.PageUtil;
import com.uit3j.cloud.common.database.vo.PageVO;
import com.uit3j.cloud.common.exception.Uit3jcloudException;
import com.uit3j.cloud.common.response.ResponseEnum;
import com.uit3j.cloud.common.rocketmq.config.RocketMqConstant;
import com.uit3j.cloud.common.util.Json;
import com.uit3j.cloud.product.mapper.HotSearchMapper;
import com.uit3j.cloud.product.model.HotSearch;
import com.uit3j.cloud.product.service.HotSearchService;
import com.uit3j.cloud.product.vo.HotSearchVO;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 热搜
 *
 * @author tan
 * @date 2021-01-27 09:10:00
 */
@Service
public class HotSearchServiceImpl implements HotSearchService {

    @Autowired
    private HotSearchMapper hotSearchMapper;

    @Autowired
    private RocketMQTemplate hotSearchMqTemplate;

    private static final Logger log = LoggerFactory.getLogger(HotSearchServiceImpl.class);

    @Override
    public PageVO<HotSearchVO> page(PageDTO pageDTO, HotSearchDTO hotSearchDTO) {
        return PageUtil.doPage(pageDTO, () -> hotSearchMapper.list(hotSearchDTO));
    }

    @Override
    public HotSearchVO getByHotSearchId(Long hotSearchId) {
        HotSearchVO sendHotSearch = hotSearchMapper.getByHotSearchId(hotSearchId);
        // 一个小时后解锁热搜列表
        SendStatus sendStatus = hotSearchMqTemplate.syncSend(RocketMqConstant.HOT_SEARCH_TOPIC, new GenericMessage<>(sendHotSearch), RocketMqConstant.TIMEOUT, RocketMqConstant.HOT_SEARCH_DELAY_LEVEL).getSendStatus();
        if (!Objects.equals(sendStatus,SendStatus.SEND_OK)) {
            // 消息发不出去就抛异常，发的出去无所谓
            throw new Uit3jcloudException(ResponseEnum.EXCEPTION);
        }

        return sendHotSearch;
    }

    @Override
    public void save(HotSearch hotSearch) {
        hotSearchMapper.save(hotSearch);
//        // 新增 rocketMQ更新es数据
        SendStatus sendStatusCanal = hotSearchMqTemplate.syncSend(RocketMqConstant.HOT_SEARCH_SAVE,new GenericMessage<>(hotSearch)).getSendStatus();
        if (!Objects.equals(sendStatusCanal,SendStatus.SEND_OK)) {
            // 消息发不出去就抛异常，发的出去无所谓
            throw new Uit3jcloudException(ResponseEnum.EXCEPTION);
        }
    }

    @Override
    public void update(HotSearch hotSearch) {
        hotSearchMapper.update(hotSearch);
        // 修改  canal更新es数据
        Map dbMap = new HashMap();
        dbMap.put("table","hot_search");
        dbMap.put("database","mall4cloud_product");
        log.info("dbMap {}",Json.toJsonString(dbMap).toString());
//        SendStatus sendStatusCanal = hotSearchMqTemplate.syncSend(RocketMqConstant.CANAL_TOPIC,new GenericMessage<>( Json.toJsonString(dbMap) )).getSendStatus();

        SendStatus sendStatusCanal = hotSearchMqTemplate.syncSend(RocketMqConstant.HOT_SEARCH_UPDATE,new GenericMessage<>(hotSearch)).getSendStatus();
        if (!Objects.equals(sendStatusCanal,SendStatus.SEND_OK)) {
            // 消息发不出去就抛异常，发的出去无所谓
            throw new Uit3jcloudException(ResponseEnum.EXCEPTION);
        }
    }

    @Override
    public void deleteById(Long hotSearchId, Long shopId) {
        hotSearchMapper.deleteById(hotSearchId, shopId);
        HotSearch hotSearch = new HotSearch();
        hotSearch.setShopId(shopId);
        hotSearch.setHotSearchId(hotSearchId);
        SendStatus sendStatusCanal = hotSearchMqTemplate.syncSend(RocketMqConstant.HOT_SEARCH_DELETE,new GenericMessage<>(hotSearch)).getSendStatus();
        if (!Objects.equals(sendStatusCanal,SendStatus.SEND_OK)) {
            // 消息发不出去就抛异常，发的出去无所谓
            throw new Uit3jcloudException(ResponseEnum.EXCEPTION);
        }
    }

    @Override
    @Cacheable(cacheNames = CacheNames.HOT_SEARCH_LIST_KEY, key = "#shopId")
    public List<HotSearchVO> listByShopId(Long shopId) {
        List<HotSearchVO> sendList = hotSearchMapper.listByShopId(shopId);
        return sendList;
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.HOT_SEARCH_LIST_KEY, key = "#shopId")
    public void removeHotSearchListCache(Long shopId) {
    }

    @Override
    public EsHotSearchBO loadEsHotSearchBO(Long hotSearchId) {
        // 获取数据
        EsHotSearchBO esHotSearchBO = hotSearchMapper.loadEsHotSearchBO(hotSearchId);
        return esHotSearchBO;
    }
}
